/*
 * @lc app=leetcode.cn id=563 lang=typescript
 *
 * [563] 二叉树的坡度
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

// TODO 节点的坡度是指 | 左树节点和 - 右树节点和 |

// TODO 但是树的坡度是指 所有节点的坡度和
function findTilt(root: TreeNode | null): number {
    if (!root) return 0;
    let count = 0;
    function sumOfTree(root: TreeNode | null): number {
        if (!root) return 0;
        let left = root.left ? root.left.val + sumOfTree(root.left) : 0;
        let right = root.right ? root.right.val + sumOfTree(root.right) : 0;
        const result = Math.abs(left - right);
        count += result;
        console.log(root.val, left, right, result);
        return left + right;
    }
    sumOfTree(root);
    return count;
}

// @lc code=end
